'''
Created on 2013-11-22

@author: Nich
'''

from PyMud.Systems.NetworkMessageSystem import NetworkMessage


class NetworkAVNode(object):
    
    def __init__(self, entity_id, location, senses):
        self.id = entity_id
        self.location = location
        self.senses = senses
        

class NetworkAVSystem(object):
    '''
    classdocs
    '''
    def __init__(self, AVMessageQueue, node_factory, messanger_queue):
        self.av_message_queue = AVMessageQueue
        self.av_nodes = []
        self.node_factory = node_factory
        self.messanger_queue = messanger_queue
        

    def get_AV_nodes(self):
        self.av_nodes = self.node_factory.create_node_list(["senses", "location"])

    def distance(self, location1, location2):
        
        import math
        x1 = location1.x
        y1 = location1.y
        z1 = location1.z
        
        x2 = location2.x
        y2 = location2.y
        z2 = location2.z
        
        dist = math.sqrt((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2)
        return dist
            
    def satisfies_requirement(self, node, message, req):
        
        if len(req) == 1:
            type = req[0]
            if type == "targeted":
                return not message.target_id == None
            if type == "is_caller":
                return node.id == message.source_id
            if type == "is_target":
                return node.id == message.target_id
        if len(req) == 2:
            type, value = req
            node_dist = self.distance(node.location, message.location)
            if type == "loudness":
                return node.senses.hearing+value > node_dist
            if type == "visibility":
                return node.senses.sight+value > node_dist
                

                
    def process_AV_messages(self):
        
        while self.av_message_queue.qsize() >= 1:
            message = self.av_message_queue.get()
            self.get_AV_nodes()
            print(self.av_nodes)
            for node in self.av_nodes:
                #TODO: maybe add code to follow exits, but for now just make sure we're in the same room
                print(message, node)
                
                print("testing rooms are the same: "+message.location.room+", "+node.location.room)
                if not message.location.room == node.location.room:
                    continue
                
                print("testing requirements")
                for requirements, messages in message.message_templates:
                    reqs_satisfied = [self.satisfies_requirement(node, message, req) for req in requirements]
                    
                    if(all(reqs_satisfied)):
                        for msg in messages:
                            out_msg = NetworkMessage(node.id, msg.format(player = message.source_id, target = message.target_id, text = message.text))
                            print("sent out message, "+str(out_msg))
                            self.messanger_queue.put(out_msg)
                        break
                
                    
                
                
            
            
            
            
            
            
            
    